package cn.lyh.gulimall.product.service;

import cn.lyh.gulimall.product.dao.PmsBrandMapper;
import cn.lyh.gulimall.product.dao.PmsCategoryBrandRelationMapper;
import cn.lyh.gulimall.product.dao.PmsCategoryMapper;
import cn.lyh.gulimall.product.entity.PmsBrand;
import cn.lyh.gulimall.product.entity.PmsCategory;
import cn.lyh.gulimall.product.entity.PmsCategoryBrandRelation;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lyh
 * @version 1.0
 * @since 2021/4/6
 */
@Service
public class PmsCategoryBrandRelationService extends ServiceImpl<PmsCategoryBrandRelationMapper, PmsCategoryBrandRelation> {

	@Autowired
	PmsBrandMapper brandMapper;

	@Autowired
	PmsCategoryMapper categoryMapper;

	public void saveDetail(PmsCategoryBrandRelation categoryBrandRelation) {
		Long brandId = categoryBrandRelation.getBrandId();
		Long catelogId = categoryBrandRelation.getCatelogId();
		// 查询详细名字和分类名
		PmsBrand pmsBrand = brandMapper.selectById(brandId);
		PmsCategory pmsCategory = categoryMapper.selectById(catelogId);
		categoryBrandRelation.setBrandName(pmsBrand.getName());
		categoryBrandRelation.setCatelogName(pmsCategory.getName());
		this.baseMapper.insert(categoryBrandRelation);
	}

	public void updateBrand(Long brandId, String name) {
		PmsCategoryBrandRelation categoryBrandRelation = new PmsCategoryBrandRelation();
		categoryBrandRelation.setBrandId(brandId);
		categoryBrandRelation.setBrandName(name);
		this.update(categoryBrandRelation,
				new UpdateWrapper<PmsCategoryBrandRelation>().eq(PmsCategoryBrandRelation.COL_BRAND_ID, brandId));
	}

	public void updateCategory(Long catId, String name) {
		this.baseMapper.updateCategory(catId, name);
	}
}
